package com.zhongkun.datahome.web.controller.system;

import com.zhongkun.datahome.common.annotation.Log;
import com.zhongkun.datahome.common.core.domain.R;
import com.zhongkun.datahome.common.core.domain.entity.Tenant;
import com.zhongkun.datahome.common.enums.BusinessType;
import com.zhongkun.datahome.common.utils.SecurityUtils;
import com.zhongkun.datahome.core.service.ITenantService;
import com.zhongkun.datahome.core.service.ITenantUserService;
import com.zhongkun.datahome.service.req.IdsBody;
import com.zhongkun.datahome.service.req.cmd.tenant.TenantCmd;
import com.zhongkun.datahome.service.res.PageVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 商户信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/tenant")
public class TenantController {
    @Autowired
    private ITenantService tenantService;

    @Autowired
    private ITenantUserService tenantUserService;

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @ApiOperation("根据userId获取商户列表")
    @GetMapping("queryTenantList")
    public R<List<Tenant>> getInfo() {
        List<Tenant> tenantList = tenantUserService.queryTenantList(SecurityUtils.getUserId());
        return R.ok(tenantList);
    }

    @ApiOperation("分页查询商户信息")
    @PreAuthorize("@ss.hasPermi('core:tenant:page')")
    @GetMapping("/page")
    public R<PageVo<Tenant>> page(String tenantName, @RequestParam(defaultValue = "1") Integer pageNo, @RequestParam(defaultValue = "10") Integer pageSize) {
        return R.ok(PageVo.of(tenantService.page(tenantName, pageNo, pageSize)));
    }

    @ApiOperation("新增商户信息")
    @PreAuthorize("@ss.hasPermi('core:tenant:add')")
    @Log(title = "商户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Long> add(@Valid @RequestBody TenantCmd cmd) {
        return R.ok(tenantService.add(cmd.getTenantName()));
    }

    @ApiOperation("修改商户信息")
    @PreAuthorize("@ss.hasPermi('core:tenant:edit')")
    @Log(title = "商户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Valid @RequestBody TenantCmd cmd) {
        tenantService.update(cmd.getId(), cmd.getTenantName());
        return R.ok();
    }

//    @ApiOperation("上/下架商户")
//    @PreAuthorize("@ss.hasPermi('core:tenant:switch')")
//    @Log(title = "商户信息", businessType = BusinessType.UPDATE)
//    @PutMapping("/switch")
//    public R<Void> switchByIds(@RequestBody SwitchSkuCmd cmd) {
//        tenantService.switchByIds(cmd);
//        return R.ok();
//    }

    @ApiOperation("删除商户信息")
    @PreAuthorize("@ss.hasPermi('core:tenant:remove')")
    @Log(title = "商户信息", businessType = BusinessType.DELETE)
    @DeleteMapping
    public R<Void> remove(@RequestBody IdsBody idsBody) {
        tenantService.deleteByIds(idsBody.getIds());
        return R.ok();
    }
}
